﻿using System;
using System.Collections;
using System.Management.Automation;
using Microsoft.ResourceManagement.ObjectModel;

namespace Quest.FIMPowerShellSnapin
{
    [Cmdlet(VerbsCommon.Set, Constants.Nouns.FIMResource, SupportsShouldProcess = true)]
    public class SetIlmResource : PSCmdlet
    {
        private const String ParamSetInstance = "Instance";
        private const String ParamSetObjectID = "ObjectID";
        private const String AddParamName = "Add";
        private const String ReplaceParamName = "Replace";
        private const String RemoveParamName = "Remove";

        private Hashtable _attrsToAdd;
        private Hashtable _attrsToReplace;
        private Hashtable _attrsToRemove;
        private FIMPSResource _instance;
        private string _objectID;
        private bool _passThru;
        private FIMPSSession _session;
        private string _locale;

        [Parameter(Mandatory = true, ParameterSetName = ParamSetInstance, ValueFromPipeline = true)]
        [ValidateNotNullOrEmpty]
        public FIMPSResource Instance 
        {
            get { return _instance; }
            set { _instance = value; }
        }

        [Parameter(Mandatory = true, ParameterSetName = ParamSetObjectID, Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        [ValidateNotNullOrEmpty]
        public String ObjectID
        {
            get { return _objectID; }
            set { _objectID = value; }
        }

        [Parameter(ParameterSetName = ParamSetObjectID)]
        [ValidateNotNullOrEmpty]
        public Hashtable Add
        {
            get { return _attrsToAdd; }
            set { _attrsToAdd = value; }
        }

        [Parameter(ParameterSetName = ParamSetObjectID)]
        [ValidateNotNullOrEmpty]
        public Hashtable Replace
        {
            get { return _attrsToReplace; }
            set { _attrsToReplace = value; }
        }

        [Parameter(ParameterSetName = ParamSetObjectID)]
        [ValidateNotNullOrEmpty]
        public Hashtable Remove
        {
            get { return _attrsToRemove; }
            set { _attrsToRemove = value; }
        }

        [Parameter]
        public SwitchParameter PassThru
        {
            get { return (SwitchParameter)_passThru; }
            set { _passThru = (bool)value; }
        }

        [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        [ValidateNotNullOrEmpty]
        public FIMPSSession Session
        {
            get { return _session; }
            set { _session = value; }
        }

        [Parameter]
        [ValidateNotNullOrEmpty]
        public String Locale 
        {
            get { return _locale; }
            set { _locale = value; }
        }

        protected override void ProcessRecord()
        {
            switch (ParameterSetName)
            {
                // A modified object has been passed in as a parameter
                case ParamSetInstance:
                    
                    break;

                //An object ID and attribute operations have been passed in on the command line
                case ParamSetObjectID:
#if false
                    RmResource rmResource = new RmResource();
                    rmResource.ObjectID = _objectID;

                    ilmRes = new ILMResource(rmObj, Session);

                    CmdletCommons.ProcessHashtable(attrsToAdd, AddParamName,
                        delegate(String attrName, Object attrValue)
                        {
                            var attr =
                                rmObj.GetOrCreateMVA(attrName, schemMgr);

                            attr.AddValues(attrValue);
                        });

                    CmdletCommons.ProcessHashtable(
                        attrsToRemove,
                        RemoveParamName,
                        delegate(String attrName, Object attrValue)
                        {
                            var attr =
                                rmObj.GetOrCreateMVA(attrName, schemMgr);

                            attr.ForceRemoveValues(attrValue);
                        });

                    CmdletCommons.ProcessHashtable(
                        attrsToReplace,
                        ReplaceParamName,
                        delegate(String attrName, Object attrValue)
                        {
                            var attr =
                                rmObj.GetOrCreateSVA(attrName, schemMgr);

                            attr.ForceChangeValue(attrValue);
                        });
#endif
                    break;

                default:
                    throw new
                        Exception(Constants.Messages.InternalError_UnknownParameterSet);
            }
#if false
            if (ilmRes != null)
            {
                bool shouldProcess = CmdletCommons.ShouldProcess(
                    this,
                    Messages.VerboseSet,
                    Messages.ConfirmSet,
                    ilmRes.RmObject);

                if (shouldProcess)
                {
                    var conFactory =
                        new ConnectionFactory(Session, SessionState);

                    var transferClient =
                        new WSTransferClient(conFactory, schemMgr);

                    transferClient.Put(ilmRes.RmObject, this.Locale);

                    if (passThru)
                    {
                        WriteObject(ilmRes);
                    }
                }
            }
#endif
            _session.Client.Put(_instance.GetTransactionChanges());
            _instance.AcceptChanges();
        }
    }
}
